package treasurer;

import home.HomePage;
import includes.Sessions;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.BKLiquidation;
import controllers.BKSubsidiary;
import controllers.Logs;
import controllers.ScheduleCollections;

public class ScheduleOfCollections extends WebPage {
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	private Model dateModel = new Model();
	private Model orModel = new Model();
	private Model nameModel = new Model();
	private Model assessmentModel = new Model();
	private Model finesModel = new Model();
	private Model collectorModel = new Model();
	private Model auditorModel = new Model();
	
	
	public ScheduleOfCollections () throws SQLException, IOException
	{
		TextField date = new TextField("date", dateModel);
		date.setRequired(true);
		
		TextField or = new TextField("or", orModel);
		or.setRequired(true);
		
		TextField name = new TextField("name", nameModel);
		name.setRequired(true);
		
		TextField assessment = new TextField("assessment", assessmentModel);
		assessment.setRequired(true);
		
		TextField fines = new TextField("fines", finesModel);
		fines.setRequired(true);
	
		TextField collector = new TextField("collector", collectorModel);
		collector.setRequired(true);
		
		TextField auditor = new TextField("auditor", auditorModel);
		auditor.setRequired(true);
		
		Form form = new Form("ScheduleOfCollectionsForm");
		
		form.add(new Button("add"){
		
		private static final long serialVersionUID = 1L;

		public void onSubmit(){
			
			try {
				String date = (String) dateModel.getObject();
				String or = (String) orModel.getObject();
				String name = (String) nameModel.getObject();
				String assessment = (String) assessmentModel.getObject();
				String fines = (String) finesModel.getObject();
				String collector = (String) collectorModel.getObject();
				String auditor = (String) auditorModel.getObject();
				
				
				String uploader = ((Sessions)getSession()).getDesc();
				
				Date today = new Date();
				Timestamp timestamp = new Timestamp(today.getTime());
				String ts = timestamp.toString();
				
				ScheduleCollections sc = new ScheduleCollections();
				sc.setDate(date);
				sc.setOrNumber(or);
				sc.setName(name);
				float newAssessment = Float.valueOf(assessment);
				sc.setAssessment(newAssessment);
				float newFines = Float.valueOf(fines);
				sc.setFines(newFines);
				sc.setCollector(collector);
				sc.setAuditor(auditor);
				sc.setUploader(uploader);
				sc.setDateCreated(ts);
				sc.setStatus("PENDING");
				sqlMap.insert("ScheduleCollections.insert", sc);
				
				
				Logs log = new Logs();
				log.setLog_activity("Schedule Of Collections: " + or);
				log.setLog_officer(uploader);
				log.setLog_time(ts);
				
				sqlMap.insert("Logs.insert", log);
			
				BookKeepersSubsidiaryList page = new BookKeepersSubsidiaryList();
				setResponsePage(page);
				error("added");
		}
			catch (SQLException e){
				 error("Fill up necessary fields");
				 e.printStackTrace();
			  } catch (IOException e) {
				e.printStackTrace();
			}
		}});
		
		add(new BookmarkablePageLink("manageForms", ManageForms.class));
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		add(new BookmarkablePageLink("home", TreasurerHome.class));
		add(new BookmarkablePageLink("view", BookKeepersSubsidiaryList.class));
		
		
		add(new FeedbackPanel("error"));
		
		form.add(date);
		form.add(or);
		form.add(name);
		form.add(assessment);
		form.add(fines);
		form.add(collector);
		form.add(auditor);
		add(form);
	}

}

